//////////////////////////排班管理维护///////////////////////////////////////////
var eventSource = [];
var panbanDlg;//唯一
var chooseDateObj;//选择的日历框的日期字符串，{year:2012,month:11,day:06}
//加载的值班人员的对象，加载一次，无需加载
var zbrySelectOptionRes;
//全局唯一，日历插件变量
var calendar;

function initPaibanWHFunc(xzqCode){
	var url = "queryPaibanPlanListByMonth.shtml";
	$.post(url,{xzqCode:$("#xzqLabel").text()},handleEventSource);
}


function handleEventSource(res){
	res = eval(res);
	for(var i = 0;i<res.length;i++){
		var titleStr = "";
		var resObj = res[i];
		if(res[i].yjcz10030.hours==8){//表示是上午
			titleStr = "上午:";
		}
		if(res[i].yjcz10030.hours==12){//表示是下午
			titleStr = "下午:";
		}
		if(res[i].yjcz10030.hours==17){//表示是夜晚
			titleStr = "夜晚:";
		}
		var startYear = res[i].yjcz10030.year +1900;
		var startMonth = res[i].yjcz10030.month;
		var startDay = res[i].yjcz10030.date;
		var endYear = res[i].yjcz10040.year +1900;
		var endMonth = res[i].yjcz10040.month;
		var endDay = res[i].yjcz10040.date;
		var event = {title:titleStr+res[i].yjcz10050,
				start:new Date(startYear,startMonth,startDay),
				end:new Date(startYear,startMonth,startDay),
				logicStart:new Date(startYear,startMonth,startDay,res[i].yjcz10030.hours,res[i].yjcz10030.minutes),
				logicEnd:new Date(endYear,endMonth,endDay,res[i].yjcz10040.hours,res[i].yjcz10040.minutes),
				name:res[i].yjcz10050,
				logicId:res[i].yjcz10000
		};
		eventSource.push(event);
	}
	initFullCalendar();
}

function initFullCalendar(){
	calendar = $('#wh_paiban').fullCalendar({
		header: {
			right: 'prev,next today',
			left: 'title',
		},
		editable: false,
		disableDragging:false,
		firstDay:1,
		events:eventSource,
		eventClick:eventClickFunc,
		weekMode:'liquid',//不固定周数，周高度可变化
		height: 520//日历的高度
	});
	//解除本身上一月的按钮绑定
	$('.fc-button-prev').unbind('click');  
	//重新绑定
	$('.fc-button-prev').bind('click', MonthPrev);  
	
	//解除本身每一项日历的点击事件
	$(".fc-widget-content").unbind('click');
	//重新绑定
	$(".fc-widget-content").bind('click',contentClick);
}

//点击event项，提示是否删除
function eventClickFunc(event, jsEvent, view){
	if(confirm("是否删除今天的排班计划?")){
		var todayArray = $('#wh_paiban').fullCalendar('clientEvents',function(e){//过滤函数，只返回点击这天的
			if(e.logicStart.getMonth() == event.logicStart.getMonth()&&e.logicStart.getFullYear()== event.logicStart.getFullYear()&&e.logicStart.getDate()== event.logicStart.getDate()){
				return e;
			}
		});
		var idArray = [];
		for(var i = 0;i<todayArray.length;i++){
			idArray.push(todayArray[i].logicId);
		}
		var idStr = idArray.join(";");
		var url = "deletePaibanPlan.shtml";
		$.post(url,{idStr:idStr},function(res){
			if(res=="true"){
				$('#wh_paiban').fullCalendar('removeEvents',function(e){//过滤函数，只返回点击这天的
					if(e.logicStart.getMonth() == event.logicStart.getMonth()&&e.logicStart.getFullYear()== event.logicStart.getFullYear()&&e.logicStart.getDate()== event.logicStart.getDate()){
						return e;
					}
				});
			}else{
				alert("删除失败!");
			}
		});
	}else{
		return ;
	}
}

//上一月的按钮事件
function MonthPrev(){
	var date = $('#wh_paiban').fullCalendar('getDate');
	var currentDate = new Date();
	if(date>currentDate){
		$('#wh_paiban').fullCalendar('prev');
	}else{
		alert("只允许对本月或者本月以后进行排班");
	}
}

//日历的点击事件
function contentClick(){
	chooseDateStr = "";
	var classStr = $(this).attr("class");
	if(classStr.indexOf("fc-other-month")<0){//表示点击的是本月的日历项
		var date = $('#wh_paiban').fullCalendar('getDate');
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var day = $(this)[0].innerText;//点击的日期
		if(new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate())<=new Date(year,month-1,Number(day))){
			chooseDateObj = {year:year,month:month,day:Number(day)};
			var todayArray = $('#wh_paiban').fullCalendar( 'clientEvents',function(e){//过滤函数，只返回点击这天的
				return e._start.getMonth() == chooseDateObj.month-1&&e._start.getFullYear()== chooseDateObj.year&&e._start.getDate()== chooseDateObj.day;
			});
			var swName = "";
			var xwName = "";
			var ywName = "";
			if(todayArray.length!=0){
				for(var i = 0;i<todayArray.length;i++){
					if(todayArray[i].title.indexOf("上午")>0){
						swName = todayArray[i].name;
					}
					if(todayArray[i].title.indexOf("下午")>0){
						xwName = todayArray[i].name;
					}
					if(todayArray[i].title.indexOf("夜晚")>0){
						ywName = todayArray[i].name;
					}
				}
			}
			showPanbanDlg(year,month,day,swName,xwName,ywName);
		}else{
			alert("只能对今天之后进行排班!");
		}
	}
}

//加载排班计划的对话框
function showPanbanDlg(year,month,day,swName,xwName,ywName){
	if(panbanDlg){//如果之前的对话框存在
		panbanDlg.close();
	}
	//对话框内容
//	var contentStr = '<label style="position:absolute;left:20px;top:40px;">请您为'+year+'年'+month+'月'+day+'日安排值班人员:</label>'+
//							'<div style="position:absolute;left:20px;top:80px;height:26px;lin-height:26px;width:360px">'+
//							'<div style="float:left;">省:</div>'+
//							'<select id="sheng_zbryPaiban" onchange="changeShenItem(this.value,"shi_zbryPaiban")" style="float:left;width:80px;"><option  selected="selected" value="" >请选择省</option></select>'+
//							'<div style="float:left;margin-left:5px;">市:</div>'+
//							'<select id="shi_zbryPaiban" onchange="changeShiItem(this.value,xian_zbryPaiban)" style="float:left;width:120px;"><option  selected="selected" value="" >请选择市</option></select>'+
//							'<div style="float:left;margin-left:5px;">县:</div>'+
//							'<select id="xian_zbryPaiban" style="float:left;width:100px;"><option  selected="selected" value="" >请选择县</option></select>'+
//							'</div>'+
//							'<label style="position:absolute;left:20px;top:120px;">上午(06:00-18:00)：</label><select class="zhibanRy" id="swRySelect" style="position:absolute;left:140px;top:120px;width:150px;"></select>'+
//							'<label style="position:absolute;left:20px;top:160px;">下午(18:00-06:00)：</label><select class="zhibanRy" id="xwRySelect" style="position:absolute;left:140px;top:160px;width:150px;"></select>'+
//							'<div id="paibanClick" style="position:absolute;bottom:30px;right:20px;width:60px;height:20px;line-height:20px;background-color:#97D7F7;text-align:center;cursor:pointer;">确定</div>';
//	
	var contentStr = '<div id="pbrydiv"><label style="position:absolute;left:20px;top:40px;">请您为'+year+'年'+month+'月'+day+'日安排值班人员:</label>'+
		'<label style="position:absolute;left:20px;top:70px;">上午(08:00-12:00)：</label><select class="zhibanRy" id="swRySelect" style="position:absolute;left:140px;top:70px;width:150px;"></select>'+
		'<label style="position:absolute;left:20px;top:110px;">下午(12:00-17:30)：</label><select class="zhibanRy" id="xwRySelect" style="position:absolute;left:140px;top:110px;width:150px;"></select>'+
		'<label style="position:absolute;left:20px;top:150px;">夜晚(17:30-08:00)：</label><select class="zhibanRy" id="ywRySelect" style="position:absolute;left:140px;top:150px;width:150px;"></select>'+
		'<div id="paibanClick" style="position:absolute;bottom:30px;right:20px;width:60px;height:20px;line-height:20px;background-color:#97D7F7;text-align:center;cursor:pointer;">确定</div></div>';
	panbanDlg =  $.dialog({
		title: '排班窗口',
		content: contentStr,
		resize: false,
		max: false,
		min:false,
		left:"0px",
		top:'250px',
		width:'300px',
		height:'200px',
		close:function(){
			$(this).remove();
			panbanDlg = null;
		},
		init:function(){
////////////////////////////排班表的值班人员加载，省市县
//		    setOption('china','sheng_zbryPaiban');
//		    $("#sheng_zbryPaiban").attr("disabled","disabled");
//		    setSltDefaultVal("sheng_zbryPaiban",'420000');
//		    changeShenItem($("#sheng_zbry").val(),'shi_zbryPaiban');
		}
	});
	
	//加载值班人员表
	if(!zbrySelectOptionRes){
		loadZbryForPaiban(swName,xwName,ywName);
	}else{
		appZbryToSelect(zbrySelectOptionRes,swName,xwName,ywName);
	}
}

function loadZbryForPaiban(swName,xwName,ywName){
	var url = "queryZbryList.shtml";
	$.post(url,{xzqCode:$("#xzqLabel").text()},function(res){
		res = eval(res);
		zbrySelectOptionRes = res;
		appZbryToSelect(res,swName,xwName,ywName);
	});
}

//值班人员的选项的填充
function appZbryToSelect(res,swName,xwName,ywName){
	var selectOptionStr = '<option value=""></option><option  value="'+res[0].yjcz09030+"&"+res[0].yjcz09000+'">'+res[0].yjcz09030+'</option>';
	for(var i=1;i<res.length;i++){
		selectOptionStr = selectOptionStr + '<option value="'+res[i].yjcz09030+"&"+res[i].yjcz09000+'">'+res[i].yjcz09030+'</option>';
	}
	////增加  $(window.parent.document).find  主要是解决嵌套iframe里面，对话框里面的元素都被上升到父级页面
	$(window.parent.document).find(".zhibanRy").html("").append(selectOptionStr);;
	
	//top.$(".zhibanRy").html("");
	//top.$(".zhibanRy").append(selectOptionStr);
	//此时，加载”确定按钮“的点击事件
	$(window.parent.document).find("#paibanClick").click(paibanClickFunc);
	if(swName!=""&&xwName!=""&&ywName!=""){
		$(window.parent.document).find("#swRySelect").val(swName);
		$(window.parent.document).find("#xwRySelect").val(xwName);
		$(window.parent.document).find("#ywRySelect").val(ywName);
	}
}

//点击排班确定按钮之后的函数
function paibanClickFunc(){
	var sw = "";
	var xw = "";
	var yw = "";
	var swRyId = "";
	var xwRyId = "";
	var ywRyId = "";
	if($(window.parent.document).find("#swRySelect").val()!=""){
		var sw = $(window.parent.document).find("#swRySelect").val().split("&")[0];//值班人员在YJCZ_YJCZ09中的id
		var swRyId = $(window.parent.document).find("#swRySelect").val().split("&")[1];
	}
	if($(window.parent.document).find("#xwRySelect").val()!=""){
		var xw = $(window.parent.document).find("#xwRySelect").val().split("&")[0];
		var xwRyId = $(window.parent.document).find("#xwRySelect").val().split("&")[1];
	}
	if($(window.parent.document).find("#ywRySelect").val()!=""){
		var yw = $(window.parent.document).find("#ywRySelect").val().split("&")[0];
		var ywRyId = $(window.parent.document).find("#ywRySelect").val().split("&")[1];
	}
	//更新点击的日历项的events
	refreshClickDivEvent(sw,xw,yw,swRyId,xwRyId,ywRyId);
}

//更新日历的event
function refreshClickDivEvent(sw,xw,yw,swRyId,xwRyId,ywRyId){
	var todayArray = $('#wh_paiban').fullCalendar( 'clientEvents',function(e){//过滤函数，只返回点击这天的
		return e._start.getMonth() == chooseDateObj.month-1&&e._start.getFullYear()== chooseDateObj.year&&e._start.getDate()== chooseDateObj.day;
	});
	if(todayArray.length==0){
		var chooseDate = new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day);
		if(sw!=""){
			var swObj = {title:"上午:"+sw,
				start:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
				end:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
				logicStart:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day,8),
				logicEnd:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day,12),
				nameId:swRyId,
				name:sw
			};
			$('#wh_paiban').fullCalendar('renderEvent',swObj,true);
		}
		if(xw!=""){
			var xwObj = {title:"下午:"+xw,
					start:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
					end:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
					logicStart:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day,12),
					logicEnd:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day,17,30),
					nameId:xwRyId,
					name:xw
			};
			$('#wh_paiban').fullCalendar('renderEvent',xwObj,true);
		}
		if(yw!=""){
			var ywObj = {title:"夜晚:"+yw,
					start:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
					end:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day),
					logicStart:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day,17,30),
					logicEnd:new Date(chooseDateObj.year,chooseDateObj.month-1,chooseDateObj.day+1,8),
					nameId:ywRyId,
					name:yw
			};
			$('#wh_paiban').fullCalendar('renderEvent',ywObj,true);
		}
	}else{
		var url = "deletePaibanPlan.shtml";
		var swEvent = todayArray[0];
		var xwEvent = todayArray[1];
		var ywEvent = todayArray[2];
		if(sw!=""){
			swEvent.title="上午:"+sw;
			swEvent.name = sw;
			swEvent.nameId = swRyId;
			$('#wh_paiban').fullCalendar('updateEvent', swEvent);
		}else{
			var id = swEvent.logicId;
			$.post(url,{idStr:id},function(res){
				if(res=="true"){
					$('#wh_paiban').fullCalendar('removeEvents',function(e){//过滤函数，只返回点击这天的
						if(swEvent==e){
							return e;
						}
					});
				}
			});
		}
		if(xw!=""){
			xwEvent.title="下午:"+xw;
			xwEvent.name = xw;
			xwEvent.nameId = xwRyId;
			$('#wh_paiban').fullCalendar('updateEvent', xwEvent);
		}else{
			var id = xwEvent.logicId;
			$.post(url,{idStr:id},function(res){
				if(res=="true"){
					$('#wh_paiban').fullCalendar('removeEvents',function(e){//过滤函数，只返回点击这天的
						if(xwEvent==e){
							return e;
						}
					});
				}
			});
		}
		if(yw!=""){
			ywEvent.title="夜晚:"+yw;
			ywEvent.name = yw;
			ywEvent.nameId = ywRyId;
			$('#wh_paiban').fullCalendar('updateEvent', ywEvent);
		}else{
			var id = ywEvent.logicId;
			$.post(url,{idStr:id},function(res){
				if(res=="true"){
					$('#wh_paiban').fullCalendar('removeEvents',function(e){//过滤函数，只返回点击这天的
						if(ywEvent==e){
							return e;
						}
					});
				}
			});
		}
	}
	//更新完毕，关闭对话框
	panbanDlg.close();
}


//点击确定按钮，处理数据.允许插入今天以后的
function handleDateForToDB(){
	var eventArray = $('#wh_paiban').fullCalendar( 'clientEvents');
	var nameStr = "";
	var startTimeStr = "";
	var endTimeStr = "";
	var nameIdStr = "";
	for(var i = 0;i<eventArray.length;i++){
		var event = eventArray[i];
		if(event.nameId!=undefined){
			var name = event.name;
			var startTime = event.logicStart;
			var endTime = event.logicEnd;
			var nameId  = event.nameId;
			nameStr = nameStr + name +";";
			startTimeStr = startTimeStr + startTime + ";";
			endTimeStr = endTimeStr +endTime + ";";
			nameIdStr = nameIdStr + nameId + ";";
		}
	}
	nameStr = nameStr.substring(0,nameStr.length-1);
	startTimeStr = startTimeStr.substring(0,startTimeStr.length-1);
	endTimeStr = endTimeStr.substring(0,endTimeStr.length-1);
	nameIdStr = nameIdStr.substring(0,nameIdStr.length-1);
	insertPaibanPlanToDB(nameStr,startTimeStr,endTimeStr,nameIdStr);
}


//将结果插入到数据的函数
function insertPaibanPlanToDB(nameStr,startTimeStr,endTimeStr,nameIdStr){
	//插入数据库
	var url  = "insertPaibanPlan.shtml";
	var obj = {nameStr:nameStr,startTimeStr:startTimeStr,endTimeStr:endTimeStr,nameIdStr:nameIdStr};
	$.post(url,obj,function(res){
		if(res.indexOf("wrong")<0){
			alert("排班成功");
		}
	})
}

/////////////////////////值班记录管理维护//////////////////////////////////////////
function initZhibanjiluWHFunc(){
	
}

/////////////////////////值班表维护///////////////////////////////////////////////
function initZhibanWHFunc(){
	var url = "queryShijiZhiban.shtml";
	$.post(url,null,queryShijiZhibanCallback);
}

function queryShijiZhibanCallback(res){
	res = eval(res);
	var eventSource = [];
	for(var i = 0;i<res.length;i++){
		var titleStr = "";
		var resObj = res[i];
		if(res[i].yjcz14030.hours==8){//表示是上午
			titleStr = "上午:";
		}
		if(res[i].yjcz14030.hours==12){//表示是下午
			titleStr = "下午:";
		}
		if(res[i].yjcz14030.hours==17){//表示是夜晚
			titleStr = "夜晚:";
		}
		var startYear = res[i].yjcz14030.year +1900;
		var startMonth = res[i].yjcz14030.month;
		var startDay = res[i].yjcz14030.date;
		var endYear = res[i].yjcz14040.year +1900;
		var endMonth = res[i].yjcz14040.month;
		var endDay = res[i].yjcz14040.date;
		var event = {title:titleStr+res[i].yjcz14050,
				start:new Date(startYear,startMonth,startDay),
				end:new Date(startYear,startMonth,startDay),
				logicStart:new Date(startYear,startMonth,startDay,res[i].yjcz14030.hours,res[i].yjcz14030.minutes),
				logicEnd:new Date(endYear,endMonth,endDay,res[i].yjcz14040.hours,res[i].yjcz14040.minutes),
				name:res[i].yjcz14050,
				logicId:res[i].yjcz14000
		};
		eventSource.push(event);
	}
	initShijiZhibanFullCalendar(eventSource);
}

//初始化日历
function initShijiZhibanFullCalendar(eventSource){
	var calendar = $('#wh_zhiban').fullCalendar({
		header: {
			right: 'prev,next today',
			left: 'title',
		},
		editable: false,
		disableDragging:false,
		firstDay:1,
		events:eventSource,
		eventClick:deleteEventClickFunc,
		weekMode:'liquid',//不固定周数，周高度可变化
		height: 520//日历的高度
	});
	
	//解除本身每一项日历的点击事件
	$(".fc-widget-content").unbind('click');
}

function deleteEventClickFunc(event, jsEvent, view){
	if(confirm("是否删除今天的值班表记录?")){
		var id = event.logicId;
		var url = "deleteShijiZhibanById.shtml";
		$.post(url,{id:id},function(res){
			if(res.indexOf("true")<0){
				alert("删除失败!");
				return;
			}
			$('#wh_zhiban').fullCalendar('removeEvents',function(e){//过滤函数，只返回点击这天的
				if(e.logicId == id){
					return e;
				}
			});
			alert("删除成功!");
		});
	}
}